package com.winit.schedule.webc.support;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.winit.schedule.webc.annotation.ResponseType;

public class MethodMapping {

    String             cmd;

    ResponseType       responseType;

    Method             method;

    ControllerDelegate controllerDelegate;

    public MethodMapping(String cmd, ResponseType responseType, Method method, ControllerDelegate controllerDelegate){
        this.cmd = cmd;
        this.responseType = responseType;
        this.method = method;
        this.controllerDelegate = controllerDelegate;
    }

    public Response invoke(HttpServletRequest req, HttpServletResponse resp) throws IllegalArgumentException,
                                                                            IllegalAccessException,
                                                                            InvocationTargetException {
        Object obj = method.invoke(controllerDelegate.getController(), req, resp);

        if (ResponseType.REDIRECT.equals(this.responseType)) {
            return new RedirectResponse(this.responseType, obj);
        } else if (ResponseType.FORWARD.equals(this.responseType)) {
            return new ForwardResponse(this.responseType, obj);
        } else {
            return new JsonResponse(this.responseType, obj);
        }

    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public ControllerDelegate getControllerDelegate() {
        return controllerDelegate;
    }

    public void setControllerDelegate(ControllerDelegate controllerDelegate) {
        this.controllerDelegate = controllerDelegate;
    }

}
